# coding: utf-8

import pandas as pd
import numpy as np
# import matplotlib.pyplot as plt

# df = pd.DataFrame({'A':['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo']
#                    ,'B':['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three']
#                    ,'C':np.random.randn(8)
#                    ,'D':np.random.randn(8)
#                     })
# print(df.head())

# print(df.groupby('A').sum())
# print(df.groupby(['A', 'B'], as_index=False).mean())
# print(df.groupby(['A', 'B']).agg([np.sum, np.mean, np.std, np.median]))

# 遍历group结果
# g = df.groupby('A')
# print(g)
# print(g.get_group('bar'))

# g = df.groupby(['A', 'B'])
# print(g.get_group(('foo', 'one')))

# example
df = pd.read_csv(r'F:\Work\Python\Pandas\study-pandas\file\datas\weather_20230115134249.csv')
df.loc[:, '气温(度)']=df['气温(度)'].str.replace('℃','').astype('float64')
#print(df.head())
df['month'] = df['日期'].str[:7]
print(df.head())
data = df.groupby('month')['气温(度)'].max()
print(data)
# data.plot()

group_data = df.groupby('month').agg({'气温(度)':np.max, '相对湿度(%)':np.min, '累积雨量(mm)':np.mean})
print(group_data)
